import { ApiProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToMany,
  JoinTable,
  ManyToOne,
} from 'typeorm';
import { Department } from '../department/department.entity';
import { Position } from '../position/position.entity';
import { Role } from '../role/role.entity';

@Entity()
export class User {
  @ApiProperty({ type: Number, description: 'id' })
  @PrimaryGeneratedColumn()
  public id: number;

  @ApiProperty({ type: String, description: '用户名' })
  @Column({
    type: 'varchar',
    length: 20,
    nullable: false,
    unique: true,
    comment: '用户名',
  })
  public user_name: string;

  @ApiProperty({ type: String, description: '昵称' })
  @Column({
    type: 'varchar',
    length: 20,
    nullable: true,
    comment: '昵称',
  })
  public nick_name: string;

  @Exclude()
  @Column({ type: 'varchar', length: 255, nullable: false, comment: '密码' })
  public password: string;

  @ApiProperty({ type: String, description: '性别' })
  @Column({
    type: 'varchar',
    comment: '性别',
    nullable: true,
  })
  public sex: string;

  @ApiProperty({ type: String, description: '状态(1:正常, 0: 停用)' })
  @Column({
    type: 'varchar',
    comment: '状态(1:正常, 0: 停用)',
    nullable: true,
    default: '1',
  })
  public status: string;

  @ApiProperty({ type: String, description: '删除标志（0代表存在 1代表删除）' })
  @Column({
    type: 'varchar',
    comment: '删除标志（0代表存在 1代表删除）',
    nullable: true,
    default: '0',
  })
  public is_delete: string;

  @ApiProperty({ type: String, description: '头像' })
  @Column({ type: 'varchar', comment: '头像', default: '', nullable: true })
  public avatar: string;

  @ApiProperty({
    type: String,
    description: '邮箱',
    nullable: true,
  })
  @Column({
    type: 'varchar',
    length: 255,
    comment: '邮箱',
    default: '',
    nullable: true,
  })
  public email: string;

  @ApiProperty({ type: String, description: '手机号' })
  @Column({
    type: 'varchar',
    length: 20,
    comment: '手机号',
    nullable: true,
    default: null,
  })
  public phone: string;

  @ApiProperty({
    type: Date,
    description: '出生日期',
    nullable: true,
  })
  @Column({
    type: 'timestamp',
    comment: '出生日期',
    nullable: true,
  })
  public birthday: Date;

  @ApiProperty({
    type: String,
    description: '备注',
  })
  @Column({
    type: 'text',
    comment: '备注',
    nullable: true,
  })
  public remark: string;

  @ApiProperty({ type: Date, description: '创建时间' })
  @CreateDateColumn({
    type: 'timestamp',
    comment: '创建时间',
    nullable: true,
  })
  public created_date: Date;

  @ApiProperty({
    type: String,
    description: '创建人',
  })
  @Column({
    type: 'varchar',
    length: 20,
    comment: '创建人',
    nullable: true,
  })
  public created_by: string;

  @ApiProperty({ type: Date, description: '更新时间' })
  @UpdateDateColumn({
    type: 'timestamp',
    comment: '更新时间',
    nullable: true,
  })
  public updated_date: Date;

  @ApiProperty({
    type: String,
    description: '更新人',
  })
  @Column({
    type: 'varchar',
    length: 20,
    comment: '更新人',
    nullable: true,
  })
  public updated_by: string;

  @ApiProperty({ type: Array, description: '角色' })
  @ManyToMany(() => Role, {
    createForeignKeyConstraints: false, //避免创建外键约束
  })
  @JoinTable()
  roles: Role[];

  @ApiProperty({ type: Number, description: '部门' })
  @ManyToOne(() => Department, (dept) => dept.users, {
    createForeignKeyConstraints: false, //避免创建外键约束
  })
  dept: Department;

  @ApiProperty({ type: Number, description: '岗位' })
  @ManyToOne(() => Position, (position) => position.users, {
    createForeignKeyConstraints: false, //避免创建外键约束
  })
  public position: Position;

  constructor(partial: Partial<User>) {
    Object.assign(this, partial);
  }
}
